/*!
 * form-validation
 *
 * @package /js
 * @author Yun <majinyun@xiaohe.com>
 */

var FormValidation = function () {

	var handleValidation = function (args) {
		args.preventDefault();
		var $form = $(args.target);
		var bv = $form.data('bootstrapValidator');
		var tip = $('div.alert', $form);
		$form.bootstrapValidator('disableSubmitButtons', true);
		$form.find("button[type='submit']").text('正在保存……');
		$.post($form.attr('action'), $form.serialize(), function (result) {
			if (result.status == 1) {
				tip.addClass('alert-success').show().children('strong').text('成功：');
				tip.children('span').text(result.info);
			} else {
				tip.addClass('alert-danger').show().children('strong').text('失败：');
				tip.children('span').text(result.info);
			}
			setTimeout(function () {
				$form.find("button[type='submit']").text('保存');
				if (result.url) {
					window.location.href = result.url;
				} else if (tip.hasClass('no-refresh')) {
					tip.removeClass('alert-danger').hide();
					$form.bootstrapValidator('disableSubmitButtons', false);
				//	$form.data('bootstrapValidator').resetForm(true);
				} else {
					window.location.reload();
				}
			}, 3000);
		}, 'json');
	};

	return {
		init: function (args) {
			handleValidation(args);
		}
	};
};
